package top.hundreds.oasys.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import top.hundreds.common.crud.bean.PageParam;
import top.hundreds.common.crud.service.BaseService;
import top.hundreds.oasys.bean.converter.Converter;
import top.hundreds.oasys.bean.entity.Event;
import top.hundreds.oasys.bean.enums.EventStatus;
import top.hundreds.oasys.bean.enums.EventType;
import top.hundreds.oasys.bean.vo.EventVo;
import top.hundreds.oasys.mapper.EventMapper;
import top.hundreds.oasys.mapper.UserMapper;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (Event)表服务实现类
 *
 * @author lcf
 * @since 2021-01-29 19:15:35
 */
@Service("eventService")
public class EventService extends BaseService<Event, EventMapper> {

    @Resource
    private UserMapper userMapper;

    @Resource
    private Converter<Event, EventVo> eventVoBuilder;

    public void setStatus(Long id, Integer status, String content, Date date) {
        mapper.setStatus(id, status, content, date);
    }

    public List<Event> getCheckedList(Integer page, Integer limit, Long userId) {
        Integer offset = (page-1)*limit;
        return mapper.getCheckedList(offset, limit, userId);
    }

    public EventVo buildVo(Event event) {
        EventVo eventVo = new EventVo();
        eventVoBuilder.buildVo(event, eventVo);
        eventVo.setStatusName(EventStatus.formValue(eventVo.getStatus()).getLabel());
        eventVo.setTypeName(EventType.formValue(eventVo.getType()).getLabel());
        eventVo.setUserName(userMapper.selectById(eventVo.getUserId()).getNickname());
        eventVo.setCheckerName(userMapper.selectById(eventVo.getCheckerId()).getNickname());
        return eventVo;
    }

    public List<EventVo> getList(PageParam<Event> pageParam) {
        Page<Event> page = getPage(pageParam);
        List<Event> eventList = page.getRecords();
        if (eventList.size() == 0) {
            return null;
        }
        List<EventVo> eventVoList = new ArrayList<>(eventList.size());
        for (Event event : eventList) {
            eventVoList.add(buildVo(event));
        }
        return eventVoList;
    }
}
